package com.ggh.jinjilive.dialog.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemCreateChannelBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.home.live.LiveCategory;
import com.ggh.jinjilive.remotedata.BaseApiModule;
import com.bumptech.glide.Glide;

import java.util.List;

public class CreateChannelAdapter extends RecyclerView.Adapter<CreateChannelAdapter.ItemViewHolder> {
    private List<LiveCategory> liveCategoryList;

    private LiveCategory liveCategory;

    public void setAdd(List<LiveCategory> liveCategoryList,LiveCategory liveCategory) {
        this.liveCategoryList = liveCategoryList;
        this.liveCategory = liveCategory;
        notifyDataSetChanged();
    }

    private AddItemOnClickListener addItemOnClickListener;

    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public CreateChannelAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCreateChannelBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_create_channel, parent, false);
        return new ItemViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull CreateChannelAdapter.ItemViewHolder holder, int position) {
        LiveCategory liveCategory = liveCategoryList.get(position);
        if(null != liveCategory){
            if(null != this.liveCategory){
                if(this.liveCategory.getId() == liveCategory.getId()){
                    holder.binding.text.setTextColor(Color.parseColor("#7FA8FC"));
                }else {
                    holder.binding.text.setTextColor(Color.parseColor("#FFFFFF"));
                }
            }else {
                if(position == 0){
                    holder.binding.text.setTextColor(Color.parseColor("#7FA8FC"));
                }else {
                    holder.binding.text.setTextColor(Color.parseColor("#FFFFFF"));
                }
            }

            Glide.with(holder.binding.image).load(BaseApiModule.host + liveCategory.getIcon_select()).into(holder.binding.image);
            holder.binding.text.setText(liveCategory.getCategory_name());
            holder.binding.llChooseType.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(position,liveCategory);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == liveCategoryList ? 0 : liveCategoryList.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemCreateChannelBinding binding;

        public ItemViewHolder(@NonNull ItemCreateChannelBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}